﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace ScreenMonitor.Common
{
    public static class Util
    {

        private static object lock1 = new object();
        /// <summary>
        /// 获取全屏幕截图
        /// </summary>
        /// <returns></returns>
        public static Image ScreenToImage()
        {
            lock (lock1)
            {
                ScreenCapture sc = new ScreenCapture();
                Image baseImage = sc.CaptureScreen();
                return baseImage;
            }
        }

        private static object lock2 = new object();
        /// <summary>
        /// 将指定图像转换为字节数组
        /// </summary>
        /// <param name="img">要转换的图像</param>
        /// <param name="format">目标图像格式</param>
        /// <returns>转换成的字节数组</returns>
        public static byte[] ImageToBytes(Image img, ImageFormat format)
        {
            lock (lock2)
            {
                MemoryStream stream = new MemoryStream();
                img.Save(stream, format);
                byte[] r = stream.ToArray();
                stream.Close();
                return r;
            }
        }

        private static object lock3 = new object();
        /// <summary>
        /// 将字节数组转换为图像
        /// </summary>
        /// <param name="buffer">要转换的字节数组</param>
        /// <returns>转换成的图像</returns>
        public static Image BytesToImage(byte[] buffer)
        {
            lock (lock3)
            {

                try
                {
                    MemoryStream stream = new MemoryStream(buffer);
                    Image img = Image.FromStream(stream);
                    stream.Close();
                    return img;
                }
                catch
                {
#if DEBUG
                    throw;
#else
                    Image img;
                    img = new Bitmap(1, 1);
                    return img;
#endif
                }
            }

        }

        private static object lock4 = new object();
        /// <summary>
        /// 生成新图片
        /// </summary>
        /// <param name="SourceImg">原图</param>
        /// <param name="SizeMode">图片的缩放模式</param>
        /// <param name="SorucePoint">新图片在源图片上的起始坐标(仅Normal适用)</param>
        /// <param name="TargetSize">新图的大小(AutoSize不适用)</param>
        /// <returns>生成的新图</returns>
        public static Image GenImage(Image SourceImg, Point SorucePoint, Size TargetSize, PictureBoxSizeMode SizeMode)
        {
            lock (lock4)
            {
                Size NewSize = new Size();
                Rectangle SrcRect = new Rectangle();
                switch (SizeMode)
                {
                    case PictureBoxSizeMode.Zoom:
                        float num = Math.Min((float)(((float)SourceImg.Width) / ((float)TargetSize.Width)), (float)(((float)SourceImg.Height) / ((float)TargetSize.Height)));
                        NewSize.Width = (int)(TargetSize.Width * num);
                        NewSize.Height = (int)(TargetSize.Height * num);
                        SrcRect.X = 0;
                        SrcRect.Y = 0;
                        SrcRect.Width = SourceImg.Width;
                        SrcRect.Height = SourceImg.Height;
                        break;
                    case PictureBoxSizeMode.StretchImage:
                        SrcRect.X = 0;
                        SrcRect.Y = 0;
                        SrcRect.Width = SourceImg.Width;
                        SrcRect.Height = SourceImg.Height;
                        NewSize = TargetSize;
                        break;
                    case PictureBoxSizeMode.Normal:
                        SrcRect.Location = SorucePoint;
                        SrcRect.Size = TargetSize;
                        NewSize = TargetSize;
                        break;
                    case PictureBoxSizeMode.CenterImage:
                        SrcRect.Size = SourceImg.Size;
                        if(SourceImg.Height>TargetSize.Height)
                        {
                            SrcRect.Y = (SourceImg.Height - TargetSize.Height) / 2;
                            SrcRect.Height = TargetSize.Height;
                            NewSize.Height = TargetSize.Height;
                        }
                        else
                        {
                            SrcRect.Y = 0;
                            NewSize.Height = SrcRect.Height;
                        }
                        if(SourceImg.Width>TargetSize.Height)
                        {
                            SrcRect.X = (SourceImg.Width - TargetSize.Width) / 2;
                            SrcRect.Width = TargetSize.Width;
                            NewSize.Width = TargetSize.Width;
                        }
                        else
                        {
                            SrcRect.X = 0;
                            NewSize.Width = SrcRect.Width;
                        }
                        break;
                    case PictureBoxSizeMode.AutoSize:
                        return SourceImg;
                }

                //生成新图
                //新建一个bmp图片
                System.Drawing.Image newImage = new System.Drawing.Bitmap(NewSize.Width, NewSize.Height);
                //新建一个画板
                System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);

                //设置质量
                newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //置背景色
                newG.Clear(Color.White);
                //画图
                newG.DrawImage(SourceImg,new Rectangle(0,0,NewSize.Width,NewSize.Height), SrcRect, System.Drawing.GraphicsUnit.Pixel);
                //释放资源
                newG.Dispose();
                return newImage;
            }
        }

        private static object lock5 = new object();
        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="rnd">种子</param>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public static string GetLowerCase(Random rnd, int count)
        {
            lock (lock5)
            {
                string c = "";
                // a-z  ASCII值  97-122 
                for (int j = 0; j < count; j++)
                {
                    int i = rnd.Next(97, 123);
                    c += (char)i;
                }
                return c.ToString();
            }

        }

        private static object lock6 = new object();
        /// <summary>
        /// 取得指定文件包名称的命令字符部分
        /// </summary>
        /// <param name="pkgName"></param>
        /// <returns></returns>
        public static string GetCommandLine(string pkgName)
        {
            lock (lock6)
            {
                return pkgName.Substring(5, pkgName.Length - 5);
            }
        }

        private static object lock7 = new object();
        /// <summary>
        /// 生成文件包名称
        /// </summary>
        /// <param name="CommandLine">命令字符</param>
        /// <returns></returns>
        public static string GetpkgName(string CommandLine)
        {
            lock (lock7)
            {
                return Util.GetLowerCase(new Random(), 5) + CommandLine;
            }
        }

        private static object lock8 = new object();
        /// <summary>
        /// 将Size对象转换为字节数组
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static byte[] SizeToByte(Size size)
        {
            lock (lock8)
            {
                List<byte> b = new List<byte>();
                b.AddRange(BitConverter.GetBytes(size.Height));
                b.AddRange(BitConverter.GetBytes(size.Width));
                return b.ToArray();
            }
        }

        private static object lock9 = new object();
        /// <summary>
        /// 将字节数组转换为Size对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Size ByteToSize(byte[] data)
        {
            lock (lock9)
            {
                Size s = new Size();
                s.Height = BitConverter.ToInt32(data, 0);
                s.Width = BitConverter.ToInt32(data, 4);
                return s;
            }
        }

    }
}
